<!-- 修改客户信息 -->
<template>
  <div class="modify-customer-dialog">
    <el-dialog
      width="900px"
      class=""
      top="6vh"
      title="修改客户信息"
      :visible="$store.state.dialog.modifyCustomer"
      @close="close">
      <el-row>
        <el-col :span="24">
          <el-card shadow="hover">
            <el-form :model="customerForm" :rules="rules" ref="customerForm" label-width="100px" size="mini">
              <el-row>
                <el-col :span="8">
                  <el-form-item label="公司全称">
                    <el-input v-model="customerForm.CompanyName" auto-complete="off"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="手机号" prop="Mobile1">
                    <el-input v-model="customerForm.Mobile1" auto-complete="off"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="客户名称">
                    <el-input v-model="customerForm.FullName" auto-complete="off"></el-input>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="8">
                  <el-form-item label="公司地址">
                    <el-input v-model="customerForm.Address" auto-complete="off"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="联系人">
                    <el-input v-model="customerForm.linkMan" auto-complete="off"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="联系电话" prop="Phone1">
                    <el-input v-model="customerForm.Phone1" auto-complete="off"></el-input>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="8">
                  <el-form-item label="QQ" prop="QQ">
                    <el-input v-model="customerForm.QQ" auto-complete="off"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="助记码">
                    <el-input v-model="customerForm.ZJM" auto-complete="off"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="传真">
                    <el-input v-model="customerForm.fax" auto-complete="off"></el-input>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="8">
                  <el-form-item label="上港代码" prop="protocolCode">
                    <el-input v-model="customerForm.protocolCode" auto-complete="off"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="操作员">
                    <el-select filterable v-model="customerForm.salesmanId" placeholder="请选择操作员">
                      <el-option label="全部" value=""></el-option>
                      <el-option v-for="item in salesmanList" :key="item.id" :label="item.fullName" :value="item.salesmanId"></el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
                <!-- <el-col :span="12">
                  <el-form-item label="用户名">
                    <el-input v-model="customerForm.NickName"></el-input>
                  </el-form-item>
                </el-col> -->
                <el-col :span="8">
                  <el-form-item label="是否自动接单">
                    <el-switch v-model="isAutomatic" active-color="#13ce66"></el-switch>
                  </el-form-item>
                </el-col>
              </el-row>
            </el-form>
          </el-card>
        </el-col>
      </el-row>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="sureEdit('customerForm')" size="mini">确 定</el-button>
        <el-button type="info" @click="close" size="mini">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { updateUserInfo } from '../api/customerManagement'
export default {
  data () {
    var checkQQ = (rule, value, callback) => {
      const reg = /^[1-9][0-9]{4,9}$/
      if (reg.test(value) || value === '') {
        callback()
      } else {
        return callback(new Error('请输入正确的QQ号'))
      }
    }
    // var checkPhone = (rule, value, callback) => {
    //   const reg = /^([1-9]\d{10})$/
    //   if (reg.test(value) || value === '') {
    //     callback()
    //   } else {
    //     return callback(new Error('请输入正确的手机号'))
    //   }
    // }
    // var checkPhone2 = (rule, value, callback) => {
    //   const reg = /0\d{2}-\d{7,8}/
    //   if (reg.test(value) || value === '') {
    //     callback()
    //   } else {
    //     return callback(new Error('请输入正确的座机号'))
    //   }
    // }
    return {
      rules: {
        protocolCode: [
          { required: true, message: '请输入上港代码', trigger: 'blur' }
        ],
        QQ: [
          { validator: checkQQ, trigger: 'blur' }
        ]
        // Phone1: [
        //   { validator: checkPhone2, trigger: 'blur' }
        // ],
        // Mobile1: [
        //   { validator: checkPhone, trigger: 'blur' }
        // ]
      },
      customerForm: {},
      isAutomatic: false
    }
  },
  props: {
    editObj: {
      type: String,
      default: () => {
        return ''
      }
    },
    salesmanList: {
      type: Array,
      default: () => {
        return []
      }
    }
  },
  watch: {
    '$store.state.dialog.modifyCustomer' (value) {
      if (value) {
        this.customerForm = JSON.parse(this.editObj)
        if (this.customerForm.isAutomatic === '0') {
          this.isAutomatic = true
        } else {
          this.isAutomatic = false
        }
      }
    }
  },
  methods: {
    // 修改
    async ModifyCustomer () {
      if (this.isAutomatic) {
        this.isAutomatic = 0
      } else {
        this.isAutomatic = 1
      }
      const res = await updateUserInfo({
        customerId: this.customerForm.UserId, // 客户Id
        companyName: this.customerForm.CompanyName, // 公司全称
        mobile1: this.customerForm.Mobile1, // 手机号
        fullName: this.customerForm.FullName, // 客户名称
        address: this.customerForm.Address, // 公司地址
        linkMan: this.customerForm.linkMan, // 联系人
        phone1: this.customerForm.Phone1, // 联系电话
        qq: this.customerForm.QQ, // QQ号码
        protocolCode: this.customerForm.protocolCode,
        fax: this.customerForm.fax, // 传真
        ZJM: this.customerForm.ZJM, // 助记码
        salesmanId: this.customerForm.salesmanId,
        isAutomatic: this.isAutomatic
        // userName: this.customerForm.NickName.trim()
      })
      if (res.status === 200) {
        this.$notify({
          title: '成功',
          message: res.msg,
          type: 'success'
        })
        this.$parent.parentName()
        this.$store.commit('hide_modify_customer_dialog')
      } else {
        this.$notify({
          title: '错误',
          message: res.msg,
          type: 'error'
        })
        this.$parent.parentName()
      }
    },
    close () {
      this.$store.commit('hide_modify_customer_dialog')
    },
    // 确认添加
    sureEdit (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.ModifyCustomer()
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style scoped lang="less">

</style>
